package org.lalib.controller.action;

import java.util.ArrayList;
import java.util.List;

import org.lalib.acquire.IControllerAction;
import org.lalib.controller.form.CTableCategoryForm;
import org.lalib.controller.form.CTableCategoryForm.CategoryBean;
import org.lalib.controller.service.CCategoryService;
import org.lalib.controller.service.CTableService;
import org.lalib.entity.object.ECategory;
import org.lalib.entity.object.EItem;
import org.lalib.entity.object.ETable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("request")
@Controller("cTableCategoryAction")
public class CTableCategoryAction implements IControllerAction {

	@Autowired
	CCategoryService cCategoryService;

	@Autowired
	CTableCategoryForm cTableCategoryForm;

	@Autowired
	CTableService cTableService;

	private static final long serialVersionUID = 1L;

	public String init(EItem item) {
		ETable table = (ETable) item;
		resetFormVariables(table);
		return "tableCategory";
	}

	public void addToTable() {
		List<CategoryBean> selectedCategoryBeans = cTableCategoryForm.getExcludedCategories();
		ETable table = cTableCategoryForm.getTable();
		table = cTableService.getTableById(table.getId());

		boolean check = false;
		for (CategoryBean categoryBean : selectedCategoryBeans) {
			if (categoryBean.isSelected()) {
				table.getCategories().add(categoryBean.getCategory());
				check = true;
			}
		}

		if (check) {
			cTableService.update(table);
			resetFormVariables(table);
		}
	}

	public void removeFromTable() {
		List<CategoryBean> selectedCategoryBeans = cTableCategoryForm.getUserCategories();
		ETable table = cTableCategoryForm.getTable();
		table = cTableService.getTableById(table.getId());

		boolean check = false;
		for (CategoryBean categoryBean : selectedCategoryBeans) {
			if (categoryBean.isSelected()) {
				deleteTableCategoryForCategory(table, categoryBean.getCategory());
				check = true;
			}
		}

		if (check) {
			cTableService.update(table);
			resetFormVariables(table);
		}
	}

	private void resetFormVariables(ETable table) {
		table = (ETable) cTableService.getTableById(table.getId());
		List<ECategory> userCategories = table.getCategories();
		List<ECategory> allUserCategories = cCategoryService.getAllTableCategories();
		List<ECategory> filteredCategories = cCategoryService.filterCategories(userCategories, allUserCategories);

		List<CategoryBean> userBaseCategoryBeans = getCategoryBeansForGivenCategories(userCategories);
		List<CategoryBean> excludedBaseCategoryBeans = getCategoryBeansForGivenCategories(filteredCategories);

		cTableCategoryForm.setTable(table);
		cTableCategoryForm.setUserCategories(userBaseCategoryBeans);
		cTableCategoryForm.setExcludedCategories(excludedBaseCategoryBeans);
		cTableCategoryForm.setExcludedBaseCategories(filteredCategories);
		cTableCategoryForm.setUserBaseCategories(userCategories);
	}

	private void deleteTableCategoryForCategory(ETable table, ECategory category) {
		int id = 0;
		boolean found = false;

		for (int i = 0; i < table.getCategories().size(); i++) {
			if (table.getCategories().get(i).getId().equals(category.getId())) {
				id = i;
				found = true;
			}
		}

		if (found)
			table.getCategories().remove(id);
	}

	private List<CategoryBean> getCategoryBeansForGivenCategories(List<ECategory> categories) {
		List<CategoryBean> categoryBeans = new ArrayList<>();
		for (ECategory category : categories)
			categoryBeans.add(cTableCategoryForm.new CategoryBean(category));
		return categoryBeans;
	}
}